
public class ArrayAssignments implements Assignments {
	private Assignment[] Array = null; 
	
	/**
	 * Constructor Method -
	 * constract a new ArrayAssignment object, by creating a new array and using a default array length of 0;
	 */
	public ArrayAssignments()
	{
		this.Array = new Assignment[0];
	}
	
/**
 * Constructor Method -
 * initializes a new ArrayAssignment object, by copying an existing array of objects which implements Assignment.
 * field Array will contain a new array of objects which implements Assignment, and copy the pointers of 
 * the objects in the given parameter to the new array (the cells in the new array are shallow-copied from the given input array)
 * method will check if parameter is valid.
 * @param assignments - an array of objects which implements Assignment.
 * 						a valid assignments parameter:
 * 						- not null (existing array)
 * 						- array length not zero
 * 						- all cells in array points to an Assignment - not null
 * 						- no duplicate variables of assigments exist inside assignments
 */
	public ArrayAssignments(Assignment[] assignments)
	{
//construct new ArrayAssignment object only if assignment array parameter given is valid - not null and not length zero.
//else - throw exception
		if(assignments!=null && assignments.length>0) 
		{
		this.Array = new Assignment[assignments.length];
		for(int i=0;i<assignments.length;i++) 			
		{
			if(assignments[i]==null)		// check if assignments[i] is valid - point to existing object of class assignment
				throw new RuntimeException("invalid input array ,cell "+i+" is not initialized - equals null");
//			*****************
// check if parameter assignments has duplicate variables inside. in case true - throw exception
			for(int j=0;j<Array.length && Array[j]!=null;j++)
			{
				if(assignments[i].getVar().equals(Array[j].getVar()))
					throw new RuntimeException("invalid input array, duplicate variables in assignment array");
			}
			this.Array[i]=assignments[i];
//******************		
		}
		}
		else
			throw new RuntimeException("invalid input array (null or zero length)");
	}

	public double valueOf(Variable var) {
		if(var==null)
			throw new RuntimeException("invalid input,var is not initialized");
		for(int i=0;i<this.Array.length;i++) //loop through the array until we find the variable we are looking for
			if(this.Array[i].getVar().equals(var)) 
					return this.Array[i].getValue();
	    throw new RuntimeException("Variable " + var.toString() +  " does not exist in array");
	}
	public void addAssignment(Assignment assignment) {
		if(assignment==null)
			throw new RuntimeException("invalid input array (null)");
		boolean found=false;
		for(int i=0;i<this.Array.length && !found;i++) //loop through current array and see if we already have a value for new assignment
			if(this.Array[i].getVar().equals(assignment.getVar()))
				{
					this.Array[i].setValue(assignment.getValue()); //if we found a previous assignment change value it's value  
					found=true;
				}
		
		if(!found) //if we don't have a previous assignment, add one
		{ 
			Assignment[] temp = new Assignment[this.Array.length+1];
			for(int i=0;i<this.Array.length;i++)
				temp[i]=this.Array[i];
			temp[Array.length]=assignment;
			this.Array=temp;
		}
	}
}
